package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResBaseBean;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetHomeWorkFansFollowListBean;
import com.rjwh.dingdong.client.bean.localbean.GetHomeWorkFansFollowListBean;
import com.rjwh.dingdong.client.bean.localbean.HomeWorkFansFollowListItemData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.HomeWorkFansListModel;
import com.rjwh.dingdong.client.mvp.presenter.IHomeWorkFansFollowListPresenter;
import com.rjwh.dingdong.client.mvp.view.HomeWorkFansFollowListView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author 王小珂
 * @ClassName HomeWorkFansFollowListPresenter
 * @Description
 * @date 2017-1-6
 * @history 2017-1-6 author: description:
 */

public class HomeWorkFansFollowListPresenter extends BasePresenter<HomeWorkFansFollowListView> implements IHomeWorkFansFollowListPresenter {

    private HomeWorkFansListModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public HomeWorkFansFollowListPresenter(Context context, HomeWorkFansFollowListView mMvpView) {
        super(context, mMvpView);
        model = new HomeWorkFansListModel(this);
    }

    /**
     * 我的粉丝|我的关注
     *
     * @param type
     * @param pageindex
     * @param isRefresh
     */
    @Override
    public void doGetHomeWorkFansFollowList(int type, int pageindex, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.doGetHomeWorkFansFollowList(type, pageindex, isRefresh);
    }

    /**
     * 关注/取消关注
     *
     * @param operation
     * @param fansid
     */
    @Override
    public void doHomeWorkFollowOperation(String operation, String fansid) {
        if(isViewAttached()){
            getMvpView().showLoadingDialog();
        }
        model.doHomeWorkFollowOperation(operation, fansid);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            getMvpView().dismissLoadingDialog();
            switch (businessType) {
                case NetConstant.GETHOMEWORKFANSFOLLOWLIST:
                    getMvpView().setRefreshing(false);
                    ResGetHomeWorkFansFollowListBean resGetHomeWorkFansFollowListBean = (ResGetHomeWorkFansFollowListBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetHomeWorkFansFollowListBean != null) {
                        GetHomeWorkFansFollowListBean data = resGetHomeWorkFansFollowListBean.getData();
                        if (data != null) {
                            List<HomeWorkFansFollowListItemData> homeworkfansfollowlist = data.getHomeworkfansfollowlist();
                            if (homeworkfansfollowlist != null) {
                                if (homeworkfansfollowlist.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                getMvpView().loadData(isRefresh, homeworkfansfollowlist);
                            }
                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }
                    break;
                case NetConstant.HOMEWORK_FOLLOW:
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS) {
                            getMvpView().loadFollowState();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

}
